//
//  MainViewController.swift
//  NSinaWeibo
//
//  Created by LoliQueen on 16/2/15.
//  Copyright © 2016年 LoliQueen. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //点击按钮事件
    //1、不希望外界访问该方法 ，添加private
    //2、报错-[NSinaWeibo.MainViewController plusBtnDidClick]: unrecognized selector sent to instance 0x7ff392d345e0'
    //3、原因：一旦addtarget action 对应的方法 添加了private，消息循环就找不到该方法
    //解决：在方法前添加@objc 告诉消息循环该方法是基于OC消息动态机制派发 来执行
    //点击发表微博
    @objc private func plusBtnDidClick(){
        print(__FUNCTION__)
        let publishVC = PublishViewController()
        let nav = BaseNavController(rootViewController:  publishVC)
        presentViewController(nav, animated: true, completion: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        print(tabBar.classForCoder)
        let mainTabBar = MainTabBar()
        //通过kvc方式替换tabbar
        setValue(mainTabBar, forKey: "tabBar")
        //添加按钮点击事件
        mainTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
//        print(tabBar.classForCoder)
        
        //添加子视图控制器
        addChildViewControllers()
    }
    

    //私有方法，只有本类才可以访问
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(MineTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
 //添加单个视图控制器
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {

        //设置选中状态的图片和标题的颜色
        tabBar.tintColor = UIColor.orangeColor()
        //设置导航控制器  
        
        //换成自定义的导航控制器
        let nav = BaseNavController(rootViewController: vc)
        //设置标题
        //        nav.tabBarItem.title = "首页"
        vc.title = title

        //设置图片
        vc.tabBarItem.image = UIImage(named:imageName)
//        //设置选中状态图片
//        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(.AlwaysOriginal)
//        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        
        addChildViewController(nav)
    }
}
